<?php
namespace App\Http\Controllers;

use App\Models\Course;
use App\Models\Issue;
use Input, Auth, Validator;
use App\Models\User;
use App\Models\Model;
use App\Utils\ConfigUtils;

class IssueController extends RestfulBaseController
{
    protected $modelName = 'App\Models\Issue';


    public function __construct()
    {
        parent::__construct();

    }

    public function index()
    {
        $inputs = Input::all();
        $pager = Issue::getIssueByTag($inputs);
        $course = Course::getUserByTag();
        return view('admin.issue.list', compact('pager','course'));
    }

    public function show()
    {
        $course = Course::getUserByTag();
        return view('admin.issue.item', compact('course'));
    }

    public function edit($id)
    {
        $issue = new Issue();
        $item = $issue->find($id);
        $course = Course::getUserByTag();
        return view('admin.issue.item', compact('item','course'));
    }


    protected function _updateModel(Model $issue, Array $inputs, $type)
    {
        $type = Input::get('type');

        $radio = Input::get('radio');
        $judge = Input::get('judge');
        $answer = ($type == 27) ? $radio : $judge;
        $radioOption = Input::get('radioOption');
        if($type == 27){
            $option = $radioOption;
        }else if($type == 29){
            $option = ['正确','错误'];
        }
        $option = implode("|", $option);
        $issue->course_id = Input::get('course');
        $issue->mark = 0.5;
        $issue->type = $type;
        $issue->question = Input::get('question', '');
        $issue->option = $option;
        $issue->answer = $answer;
        if (!$issue->save())
        {
            return $this->retJson(503, '操作出错!');
        }
        return $this->retJson(200, '操作成功!');
    }

    public function destroy($id)
    {
        if (ConfigUtils::inConfigIds($id, "admin.user_table_cannot_manage_ids", false))
            return $this->retError(403, '该用户禁止修改!');
        $user = new User();
        $user->id = $id;
        $user->exists = true;
        if (!$user->delete())
        {
            return $this->retJson(503, '操作出错!');
        }
        $this->appOpLog("更新数据ID:$id", $user->getAttributes());
        return $this->retJson(200, '操作成功!');
    }

}
